package cn.com.huzo.webapp.controller.master;

import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.master.EnterpriseEntity;
import cn.com.huzo.service.master.EnterpriseManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 本公司信息控制器
 *
 * @author 吴旭
 */
@Controller
public class EnterpriseController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "EnterpriseManager")
    private EnterpriseManager _manager;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到本公司一览画面
     * <p>
     * @return
     */
    @RequestMapping("master/enterprise/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("master.enterprise.list.def");
        return view;
    }

    /**
     * 本公司编辑画面
     * <p>
     * @param session
     * @param action 操作区分
     * @param enterpriseId 本公司ID
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/enterprise/{action}/{enterpriseId}/edit.html",
            method = {RequestMethod.GET})
    public ModelAndView redirectEditWnd(HttpSession session, @PathVariable(value = "action") int action,
            @PathVariable(value = "enterpriseId") Long enterpriseId) throws Exception {
        ModelAndView view = new ModelAndView();

        view.addObject("action", action);
        view.addObject("enterpriseId", enterpriseId);

        view.setViewName("master.enterprise.edit.def");
        return view;
    }

    /**
     * 弹出本公司选择画面
     * <p>
     * @return
     */
    @RequestMapping("master/enterpriseId/choice.html")
    public ModelAndView redirectEnterpriseChoiceWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.master.enterprise.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 根据标识取得本公司数据。
     *
     * @param criterion 本公司标识
     * @return 本公司数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/enterprise/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    EnterpriseEntity fetchByKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.fetchByKey(MapUtils.getLong(criterion, "HeaderId"));
    }

    /**
     * 删除本公司信息数据。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/enterprise/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.delete(criterion);
    }

    /**
     * 根据条件取得本公司信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 本公司信息分页数据
     * @throws Exception
     */
    @RequestMapping(value = "master/enterprise/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.fetchForPage(criterion);
    }

    /**
     * 保存本公司信息数据。
     * <p>
     * @param instance 要保存的本公司信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "master/enterprise/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody EnterpriseEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getEnterpriseId() > 0) { //编辑
            rowsAffected = (long) _manager.update(instance);
        } else {//新增
            rowsAffected = _manager.insert(instance);
        }

        return rowsAffected;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="导出数据处理部分">
    /**
     * 弹出导出excel输出列选择画面
     * <p>
     * @return
     */
    @RequestMapping("popup/master/enterprise/selectColoumn.html")
    public ModelAndView redirectExportExcelWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popupWnd/80_master/enterprise_select_coloumn");
        return view;
    }

    /**
     * 导出本公司表到excel
     *
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "master/enterprise/export.html",
            method = RequestMethod.POST)
    public ModelAndView exportViewExcelMaterial(HttpServletRequest request) throws Exception {
        Long Id = Long.parseLong(request.getParameter("_EnterpriseTypeId").trim());
        String enterprisetype = request.getParameter("_EnterpriseType").trim();
        //    JSONObject fieldslist = JSONObject.fromObject(request.getParameter("_FieldsList")); 
        String fieldslist = request.getParameter("_FieldsList");
        String commentlist = request.getParameter("_CommentList");
        String enterprisename = request.getParameter("_EnterpriseName");
        String enterprisecode = request.getParameter("_EnterpriseCode");

        Map<String, Object> criterion = new HashMap();
        criterion.put("EnterpriseTypeId", Id);
        criterion.put("EnterpriseName", enterprisename);
        criterion.put("EnterpriseCode", enterprisecode);

        //     criterion.put("FieldsList", fieldslist); 
        String[] fieldsArr = fieldslist.split(",");
        List<String> lineslist = new ArrayList<>();
        //循环    
        for (Integer i = 0; i < fieldsArr.length; i++) {
            criterion.put("FieldName", fieldsArr[i]);
            List<String> linesdata = _manager.fetchExportData(criterion);
            //把list变成字符串
            String str = "";
            for (Integer j = 0; j < linesdata.size(); j++) {
                if (j == linesdata.size() - 1) {
                    str += linesdata.get(j);
                } else {
                    str += linesdata.get(j) + ",";
                }
            }
            lineslist.add(str);
        }
        //     List<MaterialEntity> lineslist= _manager.fetchAllByMaterialTypeId(criterion);
        Map<String, Object> model = new HashMap();
        model.put("lineslist", lineslist);
        model.put("enterprisetype", enterprisetype);
        model.put("commentlist", commentlist);
        return new ModelAndView("EnterpriseExcelView", model);
    }
    // </editor-fold>
}
